import React from "react";
// import { bindActionCreators } from "redux";
import { bindActionCreators } from "../my-redux";

import store from "../store";
import actions from "../store/actions/count2";

const boundActions = bindActionCreators(actions, store.dispatch);

class Counter2 extends React.Component {
  unsubscribe;
  constructor(props) {
    super(props);
    this.state = { number: store.getState().count2.number };
  }
  componentDidMount() {
    this.unsubscribe = store.subscribe(() =>
      this.setState({ number: store.getState().count2.number })
    );
  }
  componentWillUnmount() {
    this.unsubscribe();
  }
  render() {
    return (
      <div>
        <p>{this.state.number}</p>
        <button onClick={() => boundActions.add2()}>+</button>
        <button onClick={() => boundActions.minus2()}>-</button>
        <button
          onClick={() => {
            setTimeout(() => {
              boundActions.add2();
            }, 1000);
          }}
        >
          1秒后加1
        </button>
      </div>
    );
  }
}

export default Counter2;
